package com.zzhou.common.library;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.zzhou.common.library.util.LogUtils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * <desc>
 * Created by The Moss on 2018/7/23.
 */

public class MyActivityManager {
    static Application mContext;

    private MyActivityManager() {
    }

    /**
     * 维护Activity 的list
     */
    private static List<Activity> mActivitys = Collections
            .synchronizedList(new LinkedList<Activity>());

    public static void instance(Application context){
        mContext = context;
        registerActivityListener();
    }
    /**
     * 注册生命周期回调
     */
    private static void registerActivityListener() {
        mContext.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                /*
                 *  监听到 Activity创建事件 将该 Activity 加入list
                 */
                pushActivity(activity);
//                LogUtils.d("onActivityCreated:" + activity.getClass().getSimpleName());
            }

            @Override
            public void onActivityStarted(Activity activity) {
                LogUtils.d("onActivityStarted " + activity.getLocalClassName());
//                LogUtils.d("onActivityStarted:" + activity.getClass().getSimpleName());

            }

            @Override
            public void onActivityResumed(Activity activity) {
//                LogUtils.d("onActivityResumed:" + activity.getClass().getSimpleName());
            }

            @Override
            public void onActivityPaused(Activity activity) {
//                LogUtils.d("onActivityPaused:" + activity.getClass().getSimpleName());

            }

            @Override
            public void onActivityStopped(Activity activity) {
//                LogUtils.d("onActivityStopped:" + activity.getClass().getSimpleName());

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
//                LogUtils.d("onActivitySaveInstanceState:" + activity.getClass().getSimpleName());

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                if (null == mActivitys && mActivitys.isEmpty()) {
                    return;
                }
                if (mActivitys.contains(activity)) {
//                    LogUtils.d("onActivityDestroyed:" + activity.getClass().getSimpleName());

                    /**
                     *  监听到 Activity销毁事件 将该Activity 从list中移除
                     */
                    popActivity(activity);
                }
            }
        });
    }
    /**
     * @param activity 作用说明 ：添加一个activity到管理里
     */
    public static void pushActivity(Activity activity) {
        mActivitys.add(activity);
        LogUtils.d("activityList:size:" + mActivitys.size());
    }
    /**
     * @return 作用说明 ：获取当前最顶部activity的实例
     */
    public static Activity getTopActivity() {
        Activity mBaseActivity = null;
        synchronized (mActivitys) {
            final int size = mActivitys.size() - 1;
            if (size < 0) {
                return null;
            }
            mBaseActivity = mActivitys.get(size);
        }
        return mBaseActivity;

    }
    /**
     * @param activity 作用说明 ：删除一个activity在管理里
     */
    public static void popActivity(Activity activity) {
        mActivitys.remove(activity);
        LogUtils.d("activityList:size:" + mActivitys.size());
    }
    /**
     * 结束指定类名的Activity
     */
    public static void finishActivity(Class<?> cls) {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }
        for (Activity activity : mActivitys) {
            if (activity.getClass().equals(cls)) {
                finishActivity(activity);
            }
        }
    }
    /**
     * 结束指定的Activity
     */
    public static void finishActivity(Activity activity) {
        if (mActivitys == null || mActivitys.isEmpty()) {
            return;
        }
        if (activity != null) {
            mActivitys.remove(activity);
            activity.finish();
            activity = null;
        }
    }
    /**
     * 结束所有Activity
     */
    public static void finishAllActivity() {
        if (mActivitys == null) {
            return;
        }
        for (Activity activity : mActivitys) {
            activity.finish();
        }
        mActivitys.clear();
    }
}